$(function(){
    $('#tt').edatagrid({
       // updateUrl: '/channelLimitModel/batchUpdate',
       // url: '/channelLimitModel/addmodel?json',
       autoSave: false,
       batchSave: true,
       onBeforeSave:function(index){
           var _self = this;
           var len= $(this).datagrid('getData').rows.length;
           var rows = $(this).datagrid('getRows');
           var totalCurrPerc = 0;
           var remainPerc = 0;
           var currPerc = retentHandler.transformToDx2(parseFloat($("td[field='currRate'] input").val()));
           for(var i = 0; i< len; i++){
                if(index == i){
                    continue;
                }
                remainPerc = retentHandler.transformToDx2(remainPerc + rows[i].currRate);
            }
            totalCurrPerc = retentHandler.transformToDx2(remainPerc + currPerc);
            if(totalCurrPerc >  MAX_PERCENT) {
                $.messager.alert('提示:','占比超过了100%，请重新调整！', null,  function(){
                    $(_self).edatagrid('editRow', index);
                    $("td[field='currRate'] input").focus();
                });
                return false;
            }     

            retentHandler.updateChart(index, currPerc, totalCurrPerc, currPercArr);
            retentHandler.updateTable(rows, index, currPerc, currPercArr);
        },
        onClickCell: function(index,field,value){

        },
        onAfterEdit: function(index, row){
            // console.log('onAfterEdit:'+ index);
        }
       
    });

    $.extend($.fn.edatagrid.methods, {
        editRow: function(jq, index){
            return jq.each(function(){
                var dg = $(this);
                var rows = dg.datagrid('getRows');
                var row = rows[index];
                var opts = $.data(this, 'edatagrid').options;
                var editIndex = opts.editIndex;
                if (editIndex != index){
                    if (dg.datagrid('validateRow', editIndex)){
                        dg.datagrid('beginEdit', index);
                        opts.editIndex = index;
                        // opts.onEdit.call(this, index, row);  
                    } else {
                        setTimeout(function(){
                            dg.datagrid('selectRow', editIndex);
                        }, 0);
                    }
                }
            });
        }
    });
});